package com.game.view.component.icon
{
    import com.dusk.util.BitmapUtil;
    import com.dusk.util.FilterUtil;
    import com.dusk.util.ResUtil;
    import com.dusk.util.TextUtil;
    import com.game.data.vo.SkillVO;
    import com.game.view.component.ComponentBase;
    
    import flash.display.Bitmap;
    
    import flash.text.TextField;
    import flash.text.TextFormatAlign;
    
    public class RoleSkillIcon extends ComponentBase
    {
        public static const NOT_LEARN:int = 0;
        public static const CAN_LEARN:int = 1;
        public static const HAS_LEARN:int = 2;
        public static const PURE_ICON:int = 3;
        public static const DISPLAY_SKILL_INFO:int = 4;
        
        private var _state:int = 0;
        
        private var _skillVo:SkillVO;
        
        private var _indicate:TextField;
        
        private var _learnText:TextField;
        
        private var _key:String;
        
        private var _icon:Bitmap;
        
        public function RoleSkillIcon()
        {
            super();
        }
        
        /**
         * 调用之前保证存在_skillVO
         */
        public function changeState(s:int = 3):void
        {
            _state = s;
            onAdded();
        }
        
        public function getState():int
        {
            return _state;
        }
        
        /**
         * 标准vo，若需要展示参数（state为Display_Info），则需要传入对应等级的vo
         * @param vo
         */
        public function setVO(vo:SkillVO):void
        {
            _skillVo = vo;
        }
        
        public function getVO():SkillVO
        {
            return _skillVo;
        }
        
        public function setSkillKey(k:String):void
        {
            _key = k;
        }
        
        override protected function beforeAdded():void
        {
            _indicate = TextUtil.creatTextField("", TextUtil.UNIVERSAL_FONT, 16, TextFormatAlign.RIGHT, 0, false, false);
            addChild(_indicate);
            _learnText = TextUtil.creatTextField("学习", TextUtil.UNIVERSAL_FONT, 16, TextFormatAlign.CENTER, 0xFFFF00, false, false);
            addChild(_learnText);
        }
        
        override protected function onAdded():void
        {
            if (!_skillVo)
            {
                return;
            }
            if (_icon)
            {
                _icon.bitmapData.dispose();
                removeChild(_icon);
                _icon = null;
            }
            _icon = ResUtil.getImageBitmap("skillIcon_" + _skillVo.skillId);
            if (!_icon) _icon = BitmapUtil.createPureBitmap(65, 65, 0xFF0000);
            _icon.width = _icon.height = 65;
            _learnText.x = 0;
            _learnText.y = 25;
            _learnText.width = _icon.width;
            _indicate.x = 0;
            _indicate.y = 45;
            _indicate.width = _icon.width;
            switch (_state)
            {
                case NOT_LEARN:
                    _indicate.visible = false;
                    _learnText.visible = false;
                    _icon.bitmapData = BitmapUtil.rgb2gray(_icon.bitmapData);
                    break;
                case CAN_LEARN:
                    _indicate.visible = false;
                    _learnText.visible = true;
                    _icon.bitmapData = BitmapUtil.rgb2gray(_icon.bitmapData);
                    FilterUtil.deleteAllFilters(_learnText);
                    FilterUtil.setGlow(_learnText, 0xFF0000, 1, 4, 4);
                    FilterUtil.clearCache();
                    break;
                case HAS_LEARN:
                    _indicate.visible = true;
                    _learnText.visible = false;
                    _indicate.text = "Lv." + _skillVo.skillLevel;
                    TextUtil.setTextColor(_indicate, 0xFFFFFF);
                    FilterUtil.deleteAllFilters(_indicate);
                    FilterUtil.setGlow(_indicate, 0, 1, 4, 4);
                    FilterUtil.clearCache();
                    break;
                case PURE_ICON:
                    _indicate.visible = false;
                    _learnText.visible = false;
                    break;
                case DISPLAY_SKILL_INFO:
                    _indicate.visible = true;
                    _learnText.visible = false;
                    _icon.bitmapData = BitmapUtil.wrapRoundRect(_icon.bitmapData, 10);
                    _indicate.text = _key;
                    TextUtil.setTextColor(_indicate, 0);
                    FilterUtil.deleteAllFilters(_indicate);
                    FilterUtil.setGlow(_indicate, 0xFFFFFF, 1, 4, 4);
                    FilterUtil.clearCache();
            }
            _learnText.height = _learnText.textHeight;
            _indicate.height = _indicate.textHeight;
            addChildAt(_icon, 0);
        }
        
        override protected function onRemoved():void
        {
            _skillVo = null;
            if (_icon)
            {
                _icon.bitmapData.dispose();
                removeChild(_icon);
                _icon = null;
            }
        }
    }
}
